package com.dzl.fee;

import com.dzl.base.controller.BaseController;
import com.dzl.constant.CommonConstants;
import com.dzl.fee.pojo.WelfareFeeOrder;
import com.dzl.fee.pojo.WelfareFeeUserSales;
import com.dzl.fee.pojo.query.WelfareFeeUserSalesQuery;
import com.dzl.fee.pojo.vo.WelfareFeeUserSalesVo;
import com.dzl.fee.service.WelfareFeeOrderService;
import com.dzl.fee.service.WelfareFeeUserSalesService;
import com.dzl.util.EmptyUtils;
import com.dzl.util.JsonResult;
import com.dzl.util.PageUtils;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 业务员管理 - 控制层
 * @author WangJian
 * @date 2021/8/3 0003
 */
@Api(value = "企业端-业务员管理", tags = "企业端-业务员管理")
@RestController
@RequestMapping("/sales")
public class SalesController extends BaseController {

    @Autowired
    private WelfareFeeUserSalesService welfareFeeUserSalesService;
    @Autowired
    private WelfareFeeOrderService welfareFeeOrderService;

    /**
     * 业务员列表
     * @date 2021/8/3 0003 17:55
     * @author wangjian
     * @param query 业务员列表查询条件
     * @return com.dzl.util.PageUtils<com.dzl.feeSales.pojo.vo.WelfareFeeUserSalesVo>
     */
    @ApiOperation(value = "企业端-业务员列表", notes = "企业端-业务员列表")
    @GetMapping("/getSalesList")
    public PageUtils<WelfareFeeUserSalesVo> getSalesList(WelfareFeeUserSalesQuery query){
        query.setEnterpriseId(getAdminInfo().getEnterpriseId());
        PageInfo<WelfareFeeUserSalesVo> pageInfo = welfareFeeUserSalesService.getSalesVoListPage(query);
        return new PageUtils<>(pageInfo);
    }

    /**
     * 保存业务员
     * @date 2021/8/3 0003 17:15
     * @author wangjian
     * @param welfareFeeUserSales 业务员信息
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @ApiOperation(value = "企业端-保存业务员", notes = "企业端-保存业务员")
    @PostMapping("/saveSales")
    public JsonResult<Integer> saveSales(@RequestBody WelfareFeeUserSales welfareFeeUserSales){
        if (EmptyUtils.isEmpty(welfareFeeUserSales.getName())){
            return JsonResult.failWithMsg("业务员姓名不能为空");
        }
        WelfareFeeUserSales sales = new WelfareFeeUserSales();
        sales.setName(welfareFeeUserSales.getName());
        sales.setStatus(CommonConstants.YES);
        List<WelfareFeeUserSales> list = welfareFeeUserSalesService.select(sales);
        if (EmptyUtils.isEmpty(welfareFeeUserSales.getId())){
            if (EmptyUtils.isNotEmpty(list)){
                return JsonResult.failWithMsg("业务员姓名重复");
            }
            welfareFeeUserSales.setStatus(CommonConstants.YES);
            welfareFeeUserSales.setCreaterName(getAdminInfo().getUserName());
            welfareFeeUserSales.setEnterpriseId(getAdminInfo().getEnterpriseId());
        }else {
            String name = welfareFeeUserSalesService.selectByPrimaryKey(welfareFeeUserSales.getId()).getName();
            if ((!welfareFeeUserSales.getName().equals(name)) && EmptyUtils.isNotEmpty(list)){
                return JsonResult.failWithMsg("业务员姓名重复");
            }
            welfareFeeUserSales.setUpdateTime(new Date());
            welfareFeeUserSales.setUpdaterName(getAdminInfo().getUserName());
        }
        return JsonResult.success(welfareFeeUserSalesService.updateSales(welfareFeeUserSales));
    }

    /**
     * 删除业务员
     * @date 2021/8/3 0003 17:19
     * @author wangjian
     * @param id 业务员ID
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @ApiOperation(value = "企业端-删除业务员", notes = "企业端-删除业务员")
    @DeleteMapping("/deleteSales")
    public JsonResult<Integer> deleteSales(Integer id){
        //查看客户是否有订单
        WelfareFeeOrder welfareFeeOrder = new WelfareFeeOrder();
        welfareFeeOrder.setSalesUserId(id);
        welfareFeeOrder.setStatus(CommonConstants.YES);
        List<WelfareFeeOrder> select = welfareFeeOrderService.select(welfareFeeOrder);
        if (EmptyUtils.isNotEmpty(select)){
            return JsonResult.failWithMsg("该业务员有关联订单，无法删除");
        }
        return JsonResult.success(welfareFeeUserSalesService.updateSalesStatus(id));
    }
}
